<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Todo List</title>
        <style>
            .done {
                color: gray;
                text-decoration: line-through;
            }
        </style>
    </head>
    <body>
        <div id="app">
            <h2>{{ title }}</h2>
            <input type="text" v-model="title" @keydown.enter="addTodo" />
            <ul v-if="todos.length">
                <li v-for="todo in todos">
                    <input type="checkbox" v-model="todo.done" />
                    <span :class="{ done: todo.done }">{{ todo.title }}</span>
                </li>
            </ul>
            <div v-else>暂无数据</div>
            <div>
                全选<input type="checkbox" v-model="allDone" />
                <span>{{ active }} / {{ all }}</span>
            </div>
            <button v-if="active < all" @click="clear">清理</button>
        </div>

        <script src="https://unpkg.com/vue@3"></script>
        <script>
            const App = {
                data() {
                    return {
                        title: '',
                        todos: [
                            { title: '吃饭', done: false },
                            { title: '睡觉', done: true },
                        ],
                    };
                },

                computed: {
                    active() {
                        return this.todos.filter((v) => !v.done).length;
                    },

                    all() {
                        return this.todos.length;
                    },

                    allDone: {
                        get: function () {
                            return this.active === 0;
                        },
                        set: function (val) {
                            this.todos.forEach((todo) => {
                                todo.done = val;
                            });
                        },
                    },
                },

                methods: {
                    addTodo() {
                        this.todos.push({
                            title: this.title,
                            done: false,
                        });
                        this.title = '';
                    },

                    clear() {
                        this.todos = this.todos.filter((v) => !v.done);
                    },
                },
            };

            Vue.createApp(App).mount('#app');
        </script>
    </body>
</html>
